const FairyEditor = CS.FairyEditor
import FindDependencyQuery from "../WindowView/FindDependencyQuery"
import ComBasicPlugin from "./ComBasic/ComBasicPlugin"
import UIWind from "../WindowView/UIWind";
import FindFailureAssets from "../WindowView/FindFailureAssets";
import CustomName from "../WindowView/CustomName";
import ControllerEditDialog from "../WindowView/ControllerEditDialog";
import CustomPlugin from "../WindowView/CustomPlugin";
import NodeThree from "../WindowView/NodeThree";
import SG, { Config, CustomCotrollerData } from "../SG";
import ToolMgr from "../ToolMgr";
const App = FairyEditor.App

export default class ToolPlugin {
    Register() {
        this.registerMenu();
        this.registerInspector()
    }

    private registerMenu() {
        let toolmenu = App.menu.GetSubMenu("tool")
        toolmenu.AddSeperator()
        toolmenu.AddItem("自定义插件", "openCustomPlugin", this.openCustomPlugin.bind(this))
        toolmenu.AddItem("查找组件依赖关系", "findDependencyQuery", this.findDependencyQuery.bind(this))
        toolmenu.AddItem("查找失效资源", "FindFailureAssets", this.FindFailureAssets.bind(this))
        if (SG.config[Config.OPENCUSTOMNAME])
            toolmenu.AddItem("扩展名称下拉框", "customName", this.customName.bind(this))

        if (SG.config[Config.OPENCONTROLLERNAME]) {
            toolmenu.AddItem("扩展控制器下拉框", "customControllerName", this.customControllerName.bind(this))
            this.updateControllerEditDialogDownDropNames();
        }

        toolmenu.AddItem("打开节点树", "OpenThree", this.OpenThree.bind(this))


        /**右键目录 */

        let contextmenu = App.libView.contextMenu;
        //contextmenu.AddSeperator()

    }

    //#region 工具
    private findDependencyQuery() {
        FindDependencyQuery.show()
    }

    private openCustomPlugin() {
        CustomPlugin.show()
    }

    private FindFailureAssets() {
        FindFailureAssets.show()
    }

    private registerInspector() {
        ComBasicPlugin.create();
    }

    private customName() {
        CustomName.show()
    }

    private customControllerName() {
        ControllerEditDialog.show()
    }

    /**刷新控制器弹窗下拉列表扩展 */
    private updateControllerEditDialogDownDropNames() {
        let controllerPanel = App.docView.GetChildAt(0).asCom.GetChild('controllerPanel')
        controllerPanel.asCom.GetChild("list").asList.onClickItem.Add(this.onControllerEditDialogDownDropListItemClick.bind(this))
    }

    private async onControllerEditDialogDownDropListItemClick(context: CS.FairyGUI.EventContext) {
        if((context.data as CS.FairyGUI.GButton).mode != 0) return
        while (true) {
            let window: CS.FairyGUI.Window = null;
            for (let index = 0; index < App.groot.numChildren; index++) {
                window = App.groot.GetChildAt(index) as CS.FairyGUI.Window
                if (window.GetType().ToString() != "FairyEditor.Dialog.ControllerEditDialog") {
                    window = null;
                    continue
                }
            }
            if (window == null || window.contentPane == null) {
                await ToolMgr.sleep(1.5)
                continue;
            }
            let name = window.contentPane.GetChild("name") as CS.FairyGUI.GComboBox;

            //自定义名称扩展
            if (SG.config[Config.OPENCONTROLLERNAME]) {
                let c_name = name.asComboBox;
                let list = ToolMgr.createGenericList<string>(CS.System.String);
                let datas = SG.config[Config.CUSTOMCONTROLLERNAME] as Record<string, CustomCotrollerData>;
                let keys = Object.keys(datas)
                keys.forEach(element => {
                    list.Add(element)
                }, this)
                c_name.values = list.ToArray()
                c_name.items = list.ToArray()
                c_name.text = context.data.name;
                c_name.onChanged.Remove(this.onControllerEditDialogNameChanged)
                c_name.onChanged.Add(this.onControllerEditDialogNameChanged)
            }
            return
        }
    }

    /**改变控制器名称时刷新控制器弹窗下拉列表 */
    private onControllerEditDialogNameChanged() {
        let datas = SG.config[Config.CUSTOMCONTROLLERNAME] as Record<string, CustomCotrollerData>;
        let window: CS.FairyGUI.Window = null;
        for (let index = 0; index < App.groot.numChildren; index++) {
            window = App.groot.GetChildAt(index) as CS.FairyGUI.Window
            if (window.GetType().ToString() != "FairyEditor.Dialog.ControllerEditDialog") {
                window = null;
                continue
            }
        }

        let name = window.contentPane.GetChild("name") as CS.FairyGUI.GComboBox;
        let pageList = window.contentPane.GetChild("pageList") as CS.FairyGUI.GList;
        let addPage = window.contentPane.GetChild("addPage") as CS.FairyGUI.GButton;
        let removePage = window.contentPane.GetChild("removePage") as CS.FairyGUI.GButton;
        let c_name = name.asComboBox;
        pageList.selectedIndex = -1;
        let data = datas[c_name.value];
        let alias = window.contentPane.GetChild("alias").asLabel;
        alias.text = data.remark;
        alias.GetChild("title").asTextInput.onFocusOut.Call();
        if (pageList.numChildren > data.values.length) {
            for (let index = pageList.numChildren - 1; index >= data.values.length; index--) {
                pageList.selectedIndex = pageList.numChildren-1;
                removePage.onClick.Call();
            }
        } else {
            for (let index = pageList.numChildren; index < data.values.length; index++) {
                addPage.onClick.Call();
            }
        }

        for (let index = 0; index < data.values.length; index++) {
            //刷新名称
            let label_name = pageList.GetChildAt(index).asButton.GetChild("name").asLabel
            let input_name = label_name.GetChild("input").asTextInput;
            input_name.inputTextField.text = data.values[index]? data.values[index] : ""
            label_name.text = data.values[index]? data.values[index] : ""
            input_name.onFocusOut.Call()

            //刷新备注
            let label_alias = pageList.GetChildAt(index).asButton.GetChild("remark").asLabel
            let input_alias = label_alias.GetChild("input").asTextInput;
            input_alias.inputTextField.text = data.values_remark[index] ? data.values_remark[index] : ""
            label_alias.text = data.values_remark[index] ? data.values_remark[index] : ""
            input_alias.onFocusOut.Call()
        }
    }

    private OpenThree() {
        NodeThree.show()
    }

    //#endregion

    //#region 右键目录
  

    //#endregion

    onDestroy() {
        let toolmenu = App.menu.GetSubMenu("tool")
        toolmenu.RemoveItem("openCustomPlugin")
        toolmenu.RemoveItem("findDependencyQuery")
        toolmenu.RemoveItem('FindFailureAssets')
        toolmenu.RemoveItem('customName')
        toolmenu.RemoveItem('customControllerName')
        toolmenu.RemoveItem('OpenThree')


        let contextmenu = App.libView.contextMenu;
        UIWind.delAll([]);
        App.consoleView.Log("插件卸载")
    }
}

